package cn.tsoft.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.tsoft.demo.Student;
import cn.tsoft.formbeam.StudentForm;
import cn.tsoft.service.StudentService;
import cn.tsoft.service.impl.StudentServiceImpl;



public class StudentListAction extends Action {
	private StudentService service = new StudentServiceImpl();

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		//list.do?format=json
		List<Student>  students =  service.getStudentList();
		StudentForm formbean = (StudentForm) form;
		if("json".equals(formbean.getFormat())){
			StringBuilder json = new StringBuilder();
			json.append('[');
			for(Student student : students){ // {id:76,name:"xxxx",age:80}
				json.append('{');
				json.append("id:").append(student.getId()).append(',');
				json.append("name:\"").append(student.getName()).append("\",");
				json.append("age:").append(student.getAge());
				json.append('}').append(',');
			}
			json.deleteCharAt(json.length()-1);
			json.append(']');
			request.setAttribute("json", json.toString());
			return mapping.findForward("jsonvideo");
		}else{			
			request.setAttribute("videos", students);
			return mapping.findForward("video");
		}
	}


	
}
